package com.lanju.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lanju.common.SpringUtils;
import com.lanju.entity.Topic;
import com.lanju.model.SAModel;
import com.lanju.service.TopicService;

@Controller
@RequestMapping("/topic")
public class TopicController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(TopicController.class);
	@Autowired
	private TopicService topicService;
	
	@RequestMapping("getTopicList")
	@ResponseBody
	public Map<String,Object> getTopicList(HttpServletRequest request,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return topicService.getTopicList(param);
	}
	@RequestMapping("saveTopic")
	@ResponseBody
	public Map<String,Object> saveTopic(HttpServletRequest request,HttpSession session,
			@RequestParam(value = "titlefile", required = false) MultipartFile titlefile){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		SAModel saModel=(SAModel) session.getAttribute("saModel");
		param.put("userId", saModel.getAdminCode());
		String url = uploadPic(titlefile);
		param.put("topic_img", url);
		return topicService.saveTopic(param);
	}
	@RequestMapping("updateTopic")
	@ResponseBody
	public Map<String,Object> updateTopic(HttpServletRequest request,HttpSession session,
			@RequestParam(value = "titlefile", required = false) MultipartFile titlefile){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		SAModel saModel=(SAModel) session.getAttribute("saModel");
		JSONObject json = JSONObject.fromObject(param);
		Topic topic=(Topic)JSONObject.toBean(json, Topic.class);
		topic.setUpdate_time(new Date());
		topic.setUpdator(saModel.getAdminCode());
		String url = uploadPic(titlefile);
		System.out.println("url==="+url);
		Topic t = (Topic) topicService.getTopic(param).get("rows");
		if (titlefile != null)
		{topic.setTopic_img(url);}
		else{
			topic.setTopic_img(t.getTopic_img());
		}
		return topicService.updateTopic(topic);
	}
	@RequestMapping("delTopic")
	@ResponseBody
	public Map<String,Object> delTopic(HttpServletRequest request,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return topicService.delTopic(param);
	}
	@RequestMapping("getTopic")
	@ResponseBody
	public Map<String,Object> getTopic(HttpServletRequest request,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		return topicService.getTopic(param);
	}

	public String uploadPic(MultipartFile titlefile){
		String picPath1 = null;
		String vmURL1 = null;
		if (titlefile != null) {
			try {
				OutputStream os1 = null;
				InputStream in1 = null;
				in1 = titlefile.getInputStream();
				String fileUrl1 = "";
				byte[] bs1 = new byte[1024];
				int len1;
				File tempFile1 = new File("/webapp/lanjufile/activity");// linux
//				 File tempFile1 = new File("D:/upFiles/activity");
				if (!tempFile1.exists()) {
					tempFile1.mkdirs();
				} else {
					LOGGER.info("文件已存在");
				}
//				String fullPath1 = "D:/upFiles/activity/" + titlefile.getOriginalFilename();
				String fullPath1 = "/webapp/lanjufile/activity/"+ titlefile.getOriginalFilename();// linux
				vmURL1 = "/dataResourceImages/activity/"+ titlefile.getOriginalFilename();
				fileUrl1 = vmURL1 ;
				os1 = new FileOutputStream(fullPath1);
				// 开始读取
				while ((len1 = in1.read(bs1)) != -1) {
					os1.write(bs1, 0, len1);
				}
				os1.close();
				in1.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return vmURL1;
		
	}
}
